<template>
    <div class="app-container">
        <el-row :gutter="20">
            <!--部门数据-->
            <el-col :span="6" :xs="24">
                <div class="head-container">
                    <el-input v-model="deptName" clearable placeholder="请输入部门名称" prefix-icon="el-icon-search"
                        size="small" style="margin-bottom: 20px" />
                </div>
                <div class="head-container">
                    <el-tree ref="tree" :data="deptOptions" :expand-on-click-node="false"
                        :filter-node-method="filterNode" :props="defaultProps" default-expand-all highlight-current
                        @node-click="handleNodeClick" />
                </div>
            </el-col>
            <!--用户数据-->
            <el-col :span="18" :xs="24">
                <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px"
                    size="small">
                    <el-form-item label="用户名称" prop="userName">
                        <el-input v-model="queryParams.userName" clearable placeholder="请输入用户名称" style="width: 240px"
                            @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item>
                        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">{{ $t('search')
                        }}</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('reset')
                        }}</el-button>
                    </el-form-item>
                </el-form>
                <el-table :border="false" v-loading="loading" :data="userList"
                    @selection-change="handleSelectionChange">
                    <el-table-column align="center" type="selection" width="50" />
                    <el-table-column key="userName" :show-overflow-tooltip="true" align="center" label="用户名称"
                        prop="userName" />
                    <el-table-column key="nickName" :show-overflow-tooltip="true" align="center" label="用户昵称"
                        prop="nickName" />
                    <el-table-column key="deptName" :show-overflow-tooltip="true" align="center" label="部门"
                        prop="dept.deptName" />
                </el-table>
                <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum"
                    :total="total" @pagination="getList" />
            </el-col>
        </el-row>
    </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser, resetUserPwd, changeUserStatus } from '@/api/system/user';
import { getToken } from '@/utils/auth';
import { treeselect } from '@/api/system/dept';
import Treeselect from '@riophae/vue-treeselect';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';

export default {
    name: 'User',
    dicts: ['sys_normal_disable', 'sys_user_sex'],
    components: { Treeselect },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 用户表格数据
            userList: null,
            // 弹出层标题
            title: '',
            // 部门树选项
            deptOptions: undefined,
            // 是否显示弹出层
            open: false,
            // 部门名称
            deptName: undefined,
            // 默认密码
            initPassword: undefined,
            // 日期范围
            dateRange: [],
            // 岗位选项
            postOptions: [],
            // 角色选项
            roleOptions: [],
            // 表单参数
            form: {},
            defaultProps: {
                children: 'children',
                label: 'label',
            },
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                userName: undefined,
                phonenumber: undefined,
                status: undefined,
                deptId: undefined,
            },
            selectData: [],
        };
    },
    watch: {
        // 根据名称筛选部门树
        deptName(val) {
            this.$refs.tree.filter(val);
        },
    },
    created() {
        this.getList();
        this.getTreeselect();
        this.getConfigKey('sys.user.initPassword').then((response) => {
            this.initPassword = response.msg;
        });
    },
    methods: {
        /** 查询用户列表 */
        getList() {
            this.loading = true;
            listUser(this.addDateRange(this.queryParams, this.dateRange)).then((response) => {
                this.userList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        /** 查询部门下拉树结构 */
        getTreeselect() {
            treeselect().then((response) => {
                this.deptOptions = response.data;
            });
        },
        // 筛选节点
        filterNode(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },
        // 节点单击事件
        handleNodeClick(data) {
            this.queryParams.deptId = data.id;
            this.handleQuery();
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = [];
            this.resetForm('queryForm');
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.selectData = selection;
        },
        //确定按钮
        selectUserConfirm() {
            return this.selectData;
        },
    },
};
</script>
